package com.example.hao.retrofitplus.utils;



import com.example.hao.retrofitplus.pojo.ResultData;

import java.io.File;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RetrofitClient {


    private static final class Type {
        public static final MediaType TYPE_FILE = MediaType.parse("application/octet-stream");
    }


    private static final String BASE_URL = "http://192.168.3.100:8080/home/";
    private final Retrofit retrofit;
    private final OkHttpClient okHttpClient;


    private final APIRequestService apiRequestService;


    /**
     * 连接超时时间
     */
    private int CONNECT_TIMEOUT = 15;
    /**
     * 读取超时时间
     */
    private int READ_TIMEOUT = 5;
    /**
     * 写出数据超时时间
     */
    private int WRITE_TIMEOUT = 5;

    public static RetrofitClient getClient() {
        return Singleton.CLIENT;
    }

    private static final class Singleton {
        static final RetrofitClient CLIENT = new RetrofitClient();
    }
    private RetrofitClient() {

        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                //配置 OkHTTP的拦截器
//                .addInterceptor()
                //配置 cookie 缓存, cookie 其实就是一个有规律的()字符串
//                .cookieJar()
                .build();


        retrofit = new Retrofit.Builder()
                //配置OKHTTPClient
                .client(okHttpClient)
                //基础服务器地址
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //GSon 转换流
                .addConverterFactory(GsonConverterFactory.create())
                //字符转换流
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        apiRequestService = retrofit.create(APIRequestService.class);

    }


    public void register(String userName, String psw, Observer<ResultData> observer) {
        apiRequestService.register(userName, psw)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    //    private ObservableTransformer<ResultData,ResultData> DEFAULT
    public Disposable register(String userName, String psw, Consumer<ResultData> onNext) {
        return apiRequestService.register(userName, psw)

//                .compose()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext);
    }

    public Disposable register(String userName, String psw, Consumer<ResultData> onNext, Consumer<Throwable> onErr) {
        return apiRequestService.register(userName, psw)

//                .compose()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onErr);
    }

    public Disposable register(String userName, String psw, Consumer<ResultData> onNext, Consumer<Throwable> onErr, Action action) {
        return apiRequestService.register(userName, psw)

//                .compose()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onErr, action);
    }

    public Disposable register(String userName, String psw, Consumer<ResultData> onNext, Consumer<Throwable> onErr, Action action, Consumer<Disposable> disposableConsumer) {
        return apiRequestService.register(userName, psw)

//                .compose()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onErr, action, disposableConsumer);
    }

    public void getUser(String userName, Consumer<ResultData> onNext) {
        Disposable subscribe = apiRequestService.getUser(userName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext);
    }

    public void getUser(String userName, Consumer<ResultData> onNext, Consumer<Throwable> onErr) {
        Disposable subscribe = apiRequestService.getUser(userName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onErr);
    }

    public void getUser(String userName, Consumer<ResultData> onNext, Consumer<Throwable> onErr, Action action) {
        Disposable subscribe = apiRequestService.getUser(userName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onErr, action);
    }

    public void getUser(String userName, Consumer<ResultData> onNext, Consumer<Throwable> onErr, Action action, Consumer<Disposable> disposableConsumer) {
        Disposable subscribe = apiRequestService.getUser(userName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onErr, action, disposableConsumer);
    }

    public void uploadIcon(String nick, File icon, Consumer<ResultData> onNext) {

        Disposable subscribe = apiRequestService.uploadIcon(nick, MultipartBody.Part.createFormData(nick, icon.getName(), RequestBody.create(Type.TYPE_FILE, icon)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext);
    }

    public void uploadIcon(String nick, File icon, Consumer<ResultData> onNext, Consumer<Throwable> onErr) {

        Disposable subscribe = apiRequestService.uploadIcon(nick, MultipartBody.Part.createFormData(nick, icon.getName(), RequestBody.create(Type.TYPE_FILE, icon)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onErr);
    }

    public void uploadIcon(String nick, File icon, Consumer<ResultData> onNext, Consumer<Throwable> onErr, Action action) {

        Disposable subscribe = apiRequestService.uploadIcon(nick, MultipartBody.Part.createFormData(nick, icon.getName(), RequestBody.create(Type.TYPE_FILE, icon)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onErr, action);
    }

    public void uploadIcon(String nick, File icon, Consumer<ResultData> onNext, Consumer<Throwable> onErr, Action action, Consumer<Disposable> disposableConsumer) {

        Disposable subscribe = apiRequestService.uploadIcon(nick, MultipartBody.Part.createFormData(nick, icon.getName(), RequestBody.create(Type.TYPE_FILE, icon)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onErr, action, disposableConsumer);
    }



}
